//
//  GKDissmissController.m
//  GKTranstionAnimation
//
//  Created by Apple on 16/11/16.
//  Copyright © 2016年 郭凯. All rights reserved.
//

#import "GKDissmissController.h"
#import "GKPresentTransitionTool.h"
#import "GKInteractiveTransitionTool.h"

@interface GKDissmissController ()<UIViewControllerTransitioningDelegate>
@property (nonatomic,strong) GKInteractiveTransitionTool* dismissInteractiveTransitionTool;
@end

@implementation GKDissmissController
- (instancetype)init{
    self = [super init];
    if (self) {//以下步骤必须实现,要不动画效果不出现
        self.transitioningDelegate = self;
        self.modalPresentationStyle = UIModalPresentationCustom;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor yellowColor];
    self.view.layer.cornerRadius = 10;
    self.view.layer.masksToBounds = YES;

    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:@"点击我消失" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin;
    [button addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
    [button sizeToFit];
    button.center = self.view.center;
    [self.view addSubview:button];
    
    /**重点*/
    self.dismissInteractiveTransitionTool = [GKInteractiveTransitionTool interactiveTransitionWithTransitionType:InteractiveTransitionTypeDismiss GestureDirection:InteractiveTransitionGestureDirectionDown];
    [self.dismissInteractiveTransitionTool addPanGestureForViewController:self];
    
}


-(void)dismiss{
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark 转场代理方法
-(id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{
    return [GKPresentTransitionTool transitionWithTransitionType:PresentTransitionTypePresent];
}


-(id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{
    
    return [GKPresentTransitionTool transitionWithTransitionType:PresentTransitionTypeDismiss];
}


- (nullable id <UIViewControllerInteractiveTransitioning>)interactionControllerForPresentation:(id <UIViewControllerAnimatedTransitioning>)animator{
    NSLog(@"%@",self.presentInteractiveTransitionTool);
    return self.presentInteractiveTransitionTool.interation? _presentInteractiveTransitionTool : nil;
}

- (nullable id <UIViewControllerInteractiveTransitioning>)interactionControllerForDismissal:(id <UIViewControllerAnimatedTransitioning>)animator{

    return self.dismissInteractiveTransitionTool.interation? _dismissInteractiveTransitionTool : nil;
}

-(void)dealloc{
    NSLog(@"%s",__func__);
}
@end
